#!/usr/bin/env python
try:
    import json
except:
    from helper import HelpJson as json
import sys
import class_set
import helper

class ResourceRecommendationMapper(helper.MapperHelper):
    def mapper(self):
        for line in sys.stdin:
            left, right, recom_tag = self.loader(line)
            if (left is not None) and (right is not None) and (recom_tag is not None):
                self.allocator(left, self.join(right, recom_tag))

    def loader(self, line):
        line = line.strip()
        node_operator = json.loads(line)
        left = None
        right = None
        recom_tag = None
        try:
            type = node_operator["type"]
            score = int(node_operator["score"])
            aid = node_operator["aid"]
            uid = node_operator["uid"]
            tm = node_operator["addtime"]
            if (type == "viewed" and score >= 8) or (type == "starlike" and score >= 8):
                left = aid
                right = uid
                recom_tag = class_set.RecomTag('resource')
                recom_tag.setTime(tm)
                recom_tag.setRelatedId(aid)
            else:
                pass
        except KeyError:
            pass
        return (left, right, recom_tag)

def main():
    rm = ResourceRecommendationMapper()
    rm.mapper()

if __name__ == '__main__':
    main()
